def menus_to_tree(menus):
    menu_tree = []
    menu_list = []
    for menu in menus:
        menu_info = {"id": menu.id, "icon": menu.icon, "name": menu.name, "api_path": menu.api_path,
                     "identical": menu.identical, "type": menu.type, "method": menu.method, "children": []}
        # 冗余后期写成序列化类
        if menu.parent:
            menu_info["pid"] = menu.parent.id
        else:
            menu_info["pid"] = 0
        menu_list.append(menu_info)
    for menu in menu_list:
        if menu["pid"] == 0:
            menu_tree.append(menu)
        else:
            for menu2 in menu_list:
                if menu["pid"] == menu2["id"]:
                    menu2["children"].append(menu)
    return menu_tree


def menus_to_tree2(menus):
    menu_tree = []
    menu_list = []
    for menu in menus:
        menu_info = {"id": menu["id"], "icon": menu.get("icon"), "name": menu.get("name"),
                     "api_path": menu.get("api_path"), "identical": menu.get("identical"), "type": menu.get("type"),
                     "method": menu.get("method"), "children": [], "pid": menu.get("pid")}
        menu_list.append(menu_info)
    for menu in menu_list:
        if menu["pid"] == 0:
            menu_tree.append(menu)
        else:
            for menu2 in menu_list:
                if menu["pid"] == menu2["id"]:
                    menu2["children"].append(menu)
    return menu_tree
